package services

import (
	"bytes"
	"encoding/json"
	log "github.com/sirupsen/logrus"
	"go_anime_picture_api/models/gitee"
	"io"
	"net/http"
)

func GetFileInfos(url string) gitee.FileInfoList {

	client := &http.Client{}
	request, _ := http.NewRequest(http.MethodGet, url, nil)
	response, _ := client.Do(request)

	log.Debugf("%v\n", response.StatusCode)
	defer response.Body.Close()
	var buffer [512]byte
	result := bytes.NewBuffer(nil)
	for {
		n, err := response.Body.Read(buffer[0:])
		result.Write(buffer[0:n])
		if err != nil && err == io.EOF {
			break
		} else if err != nil {
			panic(err)
		}
	}

	infos := gitee.FileInfoList{}
	err := json.Unmarshal([]byte(result.String()), &infos)
	if err != nil {
		panic(err)
	}

	return infos
}
